#!/usr/bin/python
# -*- coding: utf-8 -*-

#这段代码似乎也只能在linux上运行
import sys
import random
import multiprocessing
import time


def myprint(obj):
    sys.stdout.write(str(obj) + '\n')


class ActivePool(object):

    def __init__(self):
        super(ActivePool, self).__init__()
        self.mgr = multiprocessing.Manager()
        self.active = self.mgr.list()
        self.lock = multiprocessing.Lock()

    def makeActive(self, name):
        with self.lock:
            self.active.append(name)

    def makeInactive(self, name):
        with self.lock:
            self.active.remove(name)

    def __str__(self):
        with self.lock:
            return str(self.active)


def worker(s, pool):
    name = multiprocessing.current_process().name
    with s:
        pool.makeActive(name)
        myprint('Now running: %s' % str(pool))
        time.sleep(random.random())
        pool.makeInactive(name)


if __name__ == '__main__':
    pool = ActivePool()
    s = multiprocessing.Semaphore(3)
    jobs = [multiprocessing.Process(target=worker, name=str(i),
            args=(s, pool)) for i in range(10)]

    for j in jobs:
        j.start()

    for j in jobs:
        j.join()
        myprint('Now running: %s' % str(pool))
